# FILE DESCRIPTION ============================================================
# Filename:    CMakeLists.txt
# Description: CMake configuration file for A661 Server Solution
#
# Copyright (c) 2022-2023 ANSYS, Inc. ALL RIGHTS RESERVED
# This source file may be used and distributed without restriction provided
# that this copyright statement is not removed from the file and that any
# derivative work contains this copyright notice.
#
# Warranty
# ANSYS, Inc makes no warranty of any kind with regard to the
# use of this Software, either expressed or implied, including, but not
# limited to the fitness for a particular purpose.
# =============================================================================

cmake_minimum_required(VERSION 3.20)

project(A661ServerSolution)
include(CMakeOptions.txt OPTIONAL)

set(CMAKE_C_STANDARD 99)


# Configurations
set(CMAKE_CONFIGURATION_TYPES "Release;Debug" CACHE STRING "Configs" FORCE)
if(DEFINED CMAKE_BUILD_TYPE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES})
endif()

# Default install prefix
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  set(CMAKE_INSTALL_PREFIX "install" CACHE PATH "InstallPrefix" FORCE)
endif()

###############################################################################
# Options can be specified in CMakeOptions.txt file: set(<option> <value>)
#
# SERVER_SCADE_INSTALL    Path to SCADE installation directory
# SERVER_SOURCE_DIR       Path to Server sources directory
# SERVER_WINDOWING_OPENGL Subdirectory to select Windowing and OpenGL platform
# SERVER_MONITORING       ON to activate MONITORING
#
###############################################################################



###############################################################################
# External dependencies for Windows
###############################################################################

if(WIN32)

  # Global compilation options

  set(PLATFORM_SUBDIR "hosted")
  set(SERVER_WINDOWING_OPENGL "GLFW_ES2")   #renbaohua added ,set plantform to GLFW_ES2
  if(NOT DEFINED SERVER_WINDOWING_OPENGL)
    set(SERVER_WINDOWING_OPENGL "SDL_ES2")
  else()
    message(STATUS "SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

  if(MSVC)
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
    set(MANUAL_CODE_WARNINGS "/W4")
    set(GENERATED_CODE_WARNINGS "/W3")
    set(GENERATED_MC_CODE_WARNINGS "/W3")
    set(IMPORTED_LIB_OPTIONS "/ignore:4099")

  elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
    set(MANUAL_CODE_WARNINGS "-Wall")
    set(GENERATED_CODE_WARNINGS "-Wall -Wno-maybe-uninitialized")
    set(GENERATED_MC_CODE_WARNINGS "-Wall -Wno-maybe-uninitialized -Wno-format")

  elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
    set(MANUAL_CODE_WARNINGS "-Wall")
    set(GENERATED_CODE_WARNINGS "-Wall")
    set(GENERATED_MC_CODE_WARNINGS "-Wall -Wno-format -Wno-unused-function")

  endif()

  add_compile_definitions(_CONSOLE _UNICODE _CRT_SECURE_NO_WARNINGS)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=incompatible-pointer-types")
  # Compute SERVER_SCADE_INSTALL

  if(NOT DEFINED SERVER_SCADE_INSTALL)
    if(DEFINED ENV{SERVER_SCADE_INSTALL})
      set(SERVER_SCADE_INSTALL "$ENV{SERVER_SCADE_INSTALL}")
    else()
      cmake_path(SET SCADE_TOP_DIR NORMALIZE "${CMAKE_SOURCE_DIR}/../../..")
      if(EXISTS "${SCADE_TOP_DIR}/SCADE/bin/scade.exe")
        set(SERVER_SCADE_INSTALL "${SCADE_TOP_DIR}")
      elseif(EXISTS "${SCADE_TOP_DIR}/ServerDependencies")
        set(SERVER_SCADE_INSTALL "${SCADE_TOP_DIR}/ServerDependencies")
      else()
        set(SERVER_SCADE_INSTALL "D:/Program Files/ANSYS Inc/v241/SCADE")
      endif()
    endif()
  endif()
  message(STATUS "SERVER_SCADE_INSTALL=${SERVER_SCADE_INSTALL}")

  macro(set_scade_subdir varname path)
    cmake_path(APPEND ${varname} ${SERVER_SCADE_INSTALL} "${path}")
    if(NOT EXISTS "${${varname}}")
      message(FATAL_ERROR "Missing directory ${varname}=${${varname}}")
    endif()
  endmacro()

  # For partial builds, use imported libraries

  set(IMPORTED_LIBDIR "${SERVER_SCADE_INSTALL}/SCADE A661/server/lib")

  # GRAPHICAL variables: OGLX and OpenGL paths

  if(SERVER_WINDOWING_OPENGL MATCHES ".*_ES2")
    # * ES2
    set(SERVER_OPENGL "ES2")
    add_compile_definitions(ES2_DEV_ENV OPENGLENV_ANGLE)
    set_scade_subdir(OGLX_INCDIR "SCADE Display/KCG67/oglx/include")
    set_scade_subdir(OGLX_LIBDIR "SCADE Display/KCG67/oglx/dll64")
    set_scade_subdir(ANGLE_INCDIR "contrib/ANGLE/include")
    set_scade_subdir(ANGLE_LIBDIR "contrib/ANGLE/lib64")
    set(GRAPHICAL_INCLUDES "${OGLX_INCDIR}" "${ANGLE_INCDIR}")
    set(GRAPHICAL_LIBRARIES "${OGLX_LIBDIR}/OGLX_ANGLE.lib" "${ANGLE_LIBDIR}/libGLESv2.lib" "${ANGLE_LIBDIR}/libEGL.lib")
    set(GRAPHICAL_SHARED_LIBRARIES "${OGLX_LIBDIR}/OGLX_ANGLE.dll" "${ANGLE_LIBDIR}/libGLESv2.dll" "${ANGLE_LIBDIR}/libEGL.dll")
    set(ENABLE_TESTFW ON)
    set(ENABLE_MODELCOVERAGE ON)
    set(ENABLE_UAPC ON)

  elseif(SERVER_WINDOWING_OPENGL MATCHES ".*_SC2")
    # * SC2
    set(SERVER_OPENGL "SC2")
    add_compile_definitions(SC2_DEV_ENV OPENGLENV_SC2)
    set_scade_subdir(OGLX_INCDIR "SCADE Display/OGLXSC2/include")
    set_scade_subdir(OGLX_LIBDIR "SCADE Display/OGLXSC2/lib")
    set_scade_subdir(ARGUSSC2_INCDIR "contrib/ArgusSC2/include")
    set_scade_subdir(ARGUSSC2_PLATFORM_INCDIR "contrib/ArgusSC2/include/coreavi_platform_types/coreavi_platform_types64")
    set_scade_subdir(ARGUSSC2_LIBDIR "contrib/ArgusSC2/lib")
    set(GRAPHICAL_INCLUDES "${OGLX_INCDIR}" "${ARGUSSC2_INCDIR}" "${ARGUSSC2_PLATFORM_INCDIR}")
    set(GRAPHICAL_LIBRARIES "${OGLX_LIBDIR}/OGLX_sc2.lib" "${ARGUSSC2_LIBDIR}/glsc2.lib" "${ARGUSSC2_LIBDIR}/egl.lib" "${ARGUSSC2_LIBDIR}/glsc2.lib" "${ARGUSSC2_LIBDIR}/csl.lib")
    if(MSVC)
      set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
      add_link_options("/ignore:4099")
    endif()

  else()
    # * undefined OpenGL version
    message(FATAL_ERROR "Unknown OpenGL version in SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

  # WINDOWING variables

  if(SERVER_WINDOWING_OPENGL MATCHES "SDL_.*")
    # * SDL
    set(SERVER_WINDOWING "SDL")
    set_scade_subdir(SDL2_INCDIR "contrib/SDL2/include")
    if(MSVC)
      set_scade_subdir(SDL2_LIBDIR "contrib/SDL2/lib-VS-x64")
      set(SDL2_LIBRARY "SDL2-static.lib")
    else()
      set_scade_subdir(SDL2_LIBDIR "contrib/SDL2/lib-MinGW32-x64")
      set(SDL2_LIBRARY "libSDL2.a")
    endif(MSVC)
    set(WINDOWING_INCLUDES "${SDL2_INCDIR}")
    set(WINDOWING_LIBRARIES "${SDL2_LIBDIR}/${SDL2_LIBRARY}")

  elseif(SERVER_WINDOWING_OPENGL MATCHES "GLFW_.*")
    # * GLFW
    set(SERVER_WINDOWING "GLFW")
    set_scade_subdir(GLFW3_INCDIR "contrib/GLFW3/include")
    if(MSVC)
      set_scade_subdir(GLFW3_LIBDIR "contrib/GLFW3/lib-vc2019")
      set(GLFW3_LIBRARY "glfw3.lib")
    else()
      set_scade_subdir(GLFW3_LIBDIR "contrib/GLFW3/lib-mingw-w64")
      set(GLFW3_LIBRARY "libglfw3dll.a")
      # 2. 生成完整绝对路径列表
      ##  list(APPEND WINDOWING_LIBRARIES "${GLFW3_LIBDIR}/${lib}")
      #endforeach()
    endif(MSVC)
    set(WINDOWING_INCLUDES "${GLFW3_INCDIR}" )
    set(WINDOWING_LIBRARIES "${GLFW3_LIBDIR}/${GLFW3_LIBRARY}")

  else()
    # * undefined Windowing system
    message(FATAL_ERROR "Unknown Windowing system in SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

  # SYSTEM variables

  set(SYSTEM_LIBDIR "${SERVER_SCADE_INSTALL}/contrib/Msys64/mingw64/x86_64-w64-mingw32/lib")
  set(SYSTEM_LIBRARIE libwsock32.a libvfw32.a libgdi32.a libadvapi32.a libwinmm.a libuser32.a libshell32.a libversion.a libsetupapi.a libimm32.a)
  foreach(lib IN LISTS SYSTEM_LIBRARIE)
    list(APPEND SYSTEM_LIBRARIES "${SYSTEM_LIBDIR}/${lib}")
endforeach()
  list(APPEND SYSTEM_LIBRARIES  D:/Program\ Files/ANSYS\ Inc/v241/SCADE/contrib/Msys64/mingw64/x86_64-w64-mingw32/lib/libmsvcrt.a)
  if(MSVC AND MSVC_VERSION GREATER 1800)
    list(APPEND SYSTEM_LIBRARIES legacy_stdio_definitions.lib)
  endif()

endif(WIN32)

###############################################################################
# External dependencies for Unix
###############################################################################

if(UNIX)

  # Global compilation options

  set(PLATFORM_SUBDIR "hosted")

  if(NOT DEFINED SERVER_WINDOWING_OPENGL)
    set(SERVER_WINDOWING_OPENGL "SDL_ES2")
  else()
    message(STATUS "SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

  set(MANUAL_CODE_WARNINGS "-Wall")
  set(GENERATED_CODE_WARNINGS "-Wall -Wno-maybe-uninitialized -Wno-uninitialized")
  set(GENERATED_MC_CODE_WARNINGS "-Wall -Wno-maybe-uninitialized -Wno-uninitialized -Wno-format")

  cmake_path(SET SRVDEP_DIR NORMALIZE "${CMAKE_SOURCE_DIR}/../../../ServerDependencies")

  macro(set_dependencies_subdir varname path)
    cmake_path(APPEND ${varname} ${SRVDEP_DIR} "${path}")
    if(NOT EXISTS "${${varname}}")
      message(FATAL_ERROR "Missing directory ${varname}=${${varname}}")
    endif()
  endmacro()

  # GRAPHICAL variables: OGLX and OpenGL paths

  if(SERVER_WINDOWING_OPENGL MATCHES ".*_ES2")
    # * ES2
    set(SERVER_OPENGL "ES2")
    add_compile_definitions(ES2_DEV_ENV)
    set_dependencies_subdir(OGLX_INCDIR "SCADE Display/KCG67/oglx/include")
    set_dependencies_subdir(OGLX_LIBDIR "SCADE Display/KCG67/oglx/lib")
    set(GRAPHICAL_INCLUDES "${OGLX_INCDIR}")
    set(GRAPHICAL_LIBRARIES "${OGLX_LIBDIR}/libOGLXES2.a" -lGLESv2 -lEGL)

  else()
    # * undefined OpenGL version
    message(FATAL_ERROR "Unknown OpenGL version in SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

  # WINDOWING variables

  if(SERVER_WINDOWING_OPENGL MATCHES "SDL_.*")
    # * SDL
    set(SERVER_WINDOWING "SDL")
    find_package(SDL2 REQUIRED)
    set(WINDOWING_INCLUDES ${SDL2_INCLUDE_DIRS})
    set(WINDOWING_LIBRARIES ${SDL2_LIBRARIES})
    # SYSTEM variables
    set(SYSTEM_LIBRARIES -lm -ldl)

  elseif(SERVER_WINDOWING_OPENGL MATCHES "GLFW_.*")
    # * GLFW
    set(SERVER_WINDOWING "GLFW")
    set_dependencies_subdir(GLFW3_INCDIR "GLFW3/include")
    set_dependencies_subdir(GLFW3_LIBDIR "GLFW3/src")
    set(GLFW3_LIBRARY "libglfw3.a")
    # 2. 生成完整绝对路径列表
    #  foreach(lib IN LISTS GLFW3_LIBRARY)
    #    list(APPEND WINDOWING_LIBRARIES "${GLFW3_LIBDIR}/${lib}")
    #  endforeach()
    set(WINDOWING_INCLUDES "${GLFW3_INCDIR}")
    set(WINDOWING_LIBRARIES "${GLFW3_LIBDIR}/${GLFW3_LIBRARY}")
    # SYSTEM variables
    set(SYSTEM_LIBRARIES -lm -ldl -lX11 -lpthread)

  else()
    # * undefined Windowing system
    message(FATAL_ERROR "Unknown Windowing system in SERVER_WINDOWING_OPENGL=${SERVER_WINDOWING_OPENGL}")
  endif()

endif(UNIX)

###############################################################################
# Global settings
###############################################################################

if(DEFINED SERVER_SOURCE_DIR)
  message(STATUS "SERVER_SOURCE_DIR=${SERVER_SOURCE_DIR}")
  cmake_path(SET SRVDIR NORMALIZE "${SERVER_SOURCE_DIR}")
else()
  cmake_path(SET SRVDIR NORMALIZE "${CMAKE_SOURCE_DIR}/..")
endif()

if(DEFINED SERVER_MONITORING)
  message(STATUS "SERVER_MONITORING=${SERVER_MONITORING}")
  if(${SERVER_MONITORING})
    add_compile_definitions(SERVER_MONITORING)
  endif()
endif()

if(NOT DEFINED ENABLE_TESTFW)
  set(ENABLE_TESTFW OFF)
endif()

if(NOT DEFINED ENABLE_MODELCOVERAGE)
  set(ENABLE_MODELCOVERAGE OFF)
endif()

cmake_path(SET GENDIR NORMALIZE "${CMAKE_SOURCE_DIR}/gen")
cmake_path(SET GENMCDIR NORMALIZE "${CMAKE_SOURCE_DIR}/gen_mc")

include_directories(${SRVDIR}/core/includes)
include_directories(${SRVDIR}/common/includes)
include_directories(${SRVDIR}/platform/includes)
include_directories(${SRVDIR}/widgets/common/includes)
include_directories(${GRAPHICAL_INCLUDES})
include_directories(${WINDOWING_INCLUDES})

if(DEFINED GRAPHICAL_SHARED_LIBRARIES)
  install(FILES ${GRAPHICAL_SHARED_LIBRARIES} DESTINATION bin)
endif()
if(DEFINED WINDOWING_SHARED_LIBRARIES)
  install(FILES ${WINDOWING_SHARED_LIBRARIES} DESTINATION bin)
endif()

###############################################################################
# Macro utilities
###############################################################################

macro(import_library libname)
  if(WIN32 AND EXISTS "${IMPORTED_LIBDIR}/${libname}.lib")
    # Use ${libname} library
    message(STATUS "Missing ${libname} sources directory, using library: ${IMPORTED_LIBDIR}/${libname}.lib")
    add_library(${libname} STATIC IMPORTED)
    set_target_properties(${libname} PROPERTIES IMPORTED_LOCATION "${IMPORTED_LIBDIR}/${libname}.lib")
    target_link_options(${libname} INTERFACE ${IMPORTED_LIB_OPTIONS})
  else()
    # No ${libname}
    message(FATAL_ERROR "Missing ${libname} sources directory or library: ${IMPORTED_LIBDIR}/${libname}.lib")
  endif()
endmacro()

macro(target_link_libraries_twice)
  target_link_libraries(${ARGV})
  # Fix GCC circular dependencies error
  if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
    target_link_libraries(${ARGV})
  endif()
endmacro()

macro(install_bin_target)
  install(TARGETS ${ARGV0} RUNTIME DESTINATION bin OPTIONAL)
  if(DEFINED GRAPHICAL_SHARED_LIBRARIES)
    add_custom_command(TARGET ${ARGV0} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GRAPHICAL_SHARED_LIBRARIES} $<TARGET_FILE_DIR:${ARGV0}>)
  endif()
  if(DEFINED WINDOWING_SHARED_LIBRARIES)
    add_custom_command(TARGET ${ARGV0} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${WINDOWING_SHARED_LIBRARIES} $<TARGET_FILE_DIR:${ARGV0}>)
  endif()
endmacro()

###############################################################################
# Target: ServerCore library
###############################################################################

if(EXISTS "${SRVDIR}/core/sources")
  # Use ServerCore sources

  file(GLOB_RECURSE ServerCore_files ${SRVDIR}/core/*.[hc])
  source_group(TREE ${SRVDIR} FILES ${ServerCore_files})

  file(GLOB_RECURSE ServerCommonIncludes_files ${SRVDIR}/common/includes/*.h)
  source_group(TREE ${SRVDIR} FILES ${ServerCommonIncludes_files})

  add_library(ServerCore STATIC ${ServerCore_files} ${ServerCommonIncludes_files})

  if(DEFINED MANUAL_CODE_WARNINGS)
    target_compile_options(ServerCore PRIVATE ${MANUAL_CODE_WARNINGS})
  endif()

else()
  # Use imported library
  import_library(ServerCore)

endif()

###############################################################################
# Target: Widgets library
###############################################################################

if(EXISTS "${SRVDIR}/widgets")
  # Use Widgets sources

  # Manual files

  if(EXISTS "${GENDIR}/../manual_code.lst")
    file(STRINGS "${GENDIR}/../manual_code.lst" Widgets_manual_files)
  else()
    file(GLOB_RECURSE Widgets_symbolcommands_files ${SRVDIR}/symbolCommands/*.[hc])
    file(GLOB_RECURSE Widgets_widgets_files ${SRVDIR}/widgets/*.[hc])
    set(Widgets_manual_files ${Widgets_symbolcommands_files} ${Widgets_widgets_files})
  endif()

  set(Widgets_manual_include_directories)
  foreach(Wmf ${Widgets_manual_files})
    cmake_path(GET Wmf EXTENSION Wme)
    if(${Wme} MATCHES ".[hH]")
      cmake_path(REMOVE_FILENAME Wmf)
      list(APPEND Widgets_manual_include_directories ${Wmf})
    endif()
    list(REMOVE_DUPLICATES Widgets_manual_include_directories)
  endforeach()

  if(DEFINED MANUAL_CODE_WARNINGS)
    set_source_files_properties(${Widgets_manual_files} PROPERTIES COMPILE_FLAGS ${MANUAL_CODE_WARNINGS})
  endif()

  # Widgets library

  file(GLOB Widgets_gen_displaygen_files ${GENDIR}/displaygen/*.[hc])
  file(GLOB Widgets_gen_displaySGPI_SOURCES_files
    ${GENDIR}/displaygen/SGPI_Src/src/*.[hc] ${GENDIR}/displaygen/SGPI_Src/include/*.[hc])
  file(GLOB Widgets_gen_suitegen_files ${GENDIR}/suitegen/*.[hc])
  file(GLOB Widgets_gen_srvgen_files ${GENDIR}/srvgen/*.[hc])
  set(Widgets_gen_files ${Widgets_gen_displaySGPI_SOURCES_files} ${Widgets_gen_displaygen_files} ${Widgets_gen_suitegen_files} ${Widgets_gen_srvgen_files})

  if(DEFINED GENERATED_CODE_WARNINGS)
    set_source_files_properties(${Widgets_gen_files} PROPERTIES COMPILE_FLAGS ${GENERATED_CODE_WARNINGS})
  endif()

  set(Widgets_files ${Widgets_manual_files} ${Widgets_gen_files})
  source_group(TREE ${SRVDIR} FILES ${Widgets_manual_files})
  source_group(TREE ${GENDIR} FILES ${Widgets_gen_files})

  add_library(Widgets STATIC ${Widgets_files})
  target_include_directories(Widgets PRIVATE ${GENDIR}/displaygen ${GENDIR}/suitegen ${GENDIR}/srvgen ${GENDIR}/displaygen/SGPI_Src/src ${GENDIR}/displaygen/SGPI_Src/include)
  target_include_directories(Widgets PRIVATE ${Widgets_manual_include_directories})
  target_compile_definitions(Widgets PRIVATE SGPI_EXPORTS)
  target_link_libraries(Widgets ServerCore)

else()
  # Use imported library
  import_library(Widgets)

endif()

###############################################################################
# Target: WidgetsMC library (for -mc)
###############################################################################

if(${ENABLE_MODELCOVERAGE} AND EXISTS "${SRVDIR}/widgets")

  # Use Widgets MC sources

  file(GLOB Widgets_genmc_displaygen_files ${GENMCDIR}/displaygen/*.[hc])
  file(GLOB Widgets_genmc_displaySGPI_SOURCES_files
    ${GENMCDIR}/displaygen/SGPI_Src/src/*.[hc] ${GENMCDIR}/displaygen/SGPI_Src/include/*.[hc])
  file(GLOB Widgets_genmc_suitegen_files ${GENMCDIR}/suitegen/*.[hc])
  file(GLOB Widgets_genmc_srvgen_files ${GENMCDIR}/srvgen/*.[hc])
  set(Widgets_genmc_files ${Widgets_genmc_srvgen_files} ${Widgets_genmc_displaySGPI_SOURCES_files} ${Widgets_genmc_displaygen_files} ${Widgets_genmc_suitegen_files})
  list(LENGTH Widgets_genmc_files Widgets_genmc_length)

  if(${Widgets_genmc_length} GREATER 0)

    set(WidgetsMC_files ${Widgets_manual_files} ${Widgets_genmc_files})

    if(DEFINED GENERATED_MC_CODE_WARNINGS)
      set_source_files_properties(${Widgets_genmc_files} PROPERTIES COMPILE_FLAGS ${GENERATED_MC_CODE_WARNINGS})
    endif()

    source_group(TREE ${SRVDIR} FILES ${Widgets_manual_files})
    source_group(TREE ${GENDIR} FILES ${Widgets_genmc_files})

    add_library(WidgetsMC STATIC ${WidgetsMC_files})
    target_compile_definitions(WidgetsMC PUBLIC MODEL_COVERAGE TAGSET_MULTIROOT_INTEGRATION)
    target_include_directories(WidgetsMC PRIVATE ${GENMCDIR}/displaygen ${GENMCDIR}/suitegen ${GENMCDIR}/srvgen ${GENMCDIR}/displaygen/SGPI_Src/src ${GENMCDIR}/displaygen/SGPI_Src/include)
    target_include_directories(WidgetsMC PRIVATE ${Widgets_manual_include_directories})
    target_link_libraries(WidgetsMC ServerCore)

  endif()

endif()

###############################################################################
# Target: WidgetsTest library (for -testfw)
###############################################################################

if(${ENABLE_TESTFW} AND EXISTS "${SRVDIR}/widgets")

  # Use Widgets TestFW sources

  set(WidgetsTest_includes ${GENDIR}/srvgen_testfw)
  file(GLOB WidgetsTest_files ${GENDIR}/srvgen_testfw/*.[hc])
  list(LENGTH WidgetsTest_files WidgetsTest_length)

  if(${WidgetsTest_length} GREATER 0)

    if(DEFINED GENERATED_CODE_WARNINGS)
      set_source_files_properties(${WidgetsTest_files} PROPERTIES COMPILE_FLAGS ${GENERATED_CODE_WARNINGS})
    endif()

    source_group(TREE ${GENDIR} FILES ${WidgetsTest_files})

    add_library(WidgetsTest STATIC ${WidgetsTest_files})
    target_compile_definitions(WidgetsTest PUBLIC TEST_FRAMEWORK)
    target_include_directories(WidgetsTest PUBLIC ${WidgetsTest_includes})
    target_include_directories(WidgetsTest PRIVATE ${GENDIR}/displaygen ${GENDIR}/suitegen ${GENDIR}/srvgen ${GENDIR}/displaygen/SGPI_Src/src ${GENDIR}/displaygen/SGPI_Src/include)
    target_include_directories(WidgetsTest PRIVATE ${Widgets_manual_include_directories})

  endif()

endif()

###############################################################################
# Target: WidgetsTestMC library (for -testfw and -mc)
###############################################################################

if(${ENABLE_TESTFW} AND ${ENABLE_MODELCOVERAGE} AND EXISTS "${SRVDIR}/widgets")

  # Use Widgets TestFW MC sources

  set(WidgetsTestMC_includes ${GENMCDIR}/srvgen_testfw)
  file(GLOB WidgetsTestMC_files ${GENMCDIR}/srvgen_testfw/*.[hc])
  list(LENGTH WidgetsTestMC_files WidgetsTestMC_length)

  if(${WidgetsTestMC_length} GREATER 0)

    if(DEFINED GENERATED_MC_CODE_WARNINGS)
      set_source_files_properties(${WidgetsTestMC_files} PROPERTIES COMPILE_FLAGS ${GENERATED_MC_CODE_WARNINGS})
    endif()

    source_group(TREE ${GENMCDIR} FILES ${WidgetsTestMC_files})

    add_library(WidgetsTestMC STATIC ${WidgetsTestMC_files})
    target_compile_definitions(WidgetsTestMC PUBLIC TEST_FRAMEWORK)
    target_include_directories(WidgetsTestMC PUBLIC ${WidgetsTestMC_includes})
    target_include_directories(WidgetsTestMC PRIVATE ${GENMCDIR}/displaygen ${GENMCDIR}/suitegen ${GENMCDIR}/srvgen ${GENMCDIR}/displaygen/SGPI_Src/src ${GENMCDIR}/displaygen/SGPI_Src/include)
    target_include_directories(WidgetsTestMC PRIVATE ${Widgets_manual_include_directories})

  endif()

endif()

###############################################################################
# Target: ServerPlatform library
###############################################################################

file(GLOB ServerPlatform_files ${SRVDIR}/platform/${PLATFORM_SUBDIR}/*.[hc] ${SRVDIR}/platform/${PLATFORM_SUBDIR}/${SERVER_WINDOWING_OPENGL}/*.[hc])
list(FILTER ServerPlatform_files EXCLUDE REGEX "exemain.c")
list(LENGTH ServerPlatform_files ServerPlatform_length)

if(${ServerPlatform_length} GREATER 0)
  # Use Platform sources

  if(DEFINED MANUAL_CODE_WARNINGS)
    set_source_files_properties(${ServerPlatform_files} PROPERTIES COMPILE_FLAGS ${MANUAL_CODE_WARNINGS})
  endif()

  source_group(TREE ${SRVDIR}/platform FILES ${ServerPlatform_files})

  add_library(ServerPlatform STATIC ${ServerPlatform_files})

  target_include_directories(ServerPlatform PRIVATE ${SRVDIR}/platform/${PLATFORM_SUBDIR})

else()
  # Use imported library
  import_library(ServerPlatform)

endif()

###############################################################################
# Target: A661Server executable
###############################################################################

add_executable(A661Server ${SRVDIR}/platform/${PLATFORM_SUBDIR}/exemain.c)
target_link_libraries_twice(A661Server ServerPlatform ServerCore Widgets)
target_link_libraries(A661Server ${GRAPHICAL_LIBRARIES} ${WINDOWING_LIBRARIES} ${SYSTEM_LIBRARIES})
message("The list is: ${MY_LIST} ${WINDOWING_LIBRARIES} ${SYSTEM_LIBRARIES}")
install_bin_target(A661Server)

if(MSVC)
  # Default startup project
  set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT A661Server)
endif()

###############################################################################
# Disabled target: A661ServerMC executable (for -mc)
###############################################################################

if(0 AND TARGET ServerCore AND TARGET WidgetsMC AND EXISTS "${SRVDIR}/platform/${PLATFORM_SUBDIR}")

  add_executable(A661ServerMC ${SRVDIR}/platform/${PLATFORM_SUBDIR}/exemain.c ${ServerPlatform_files})
  target_compile_definitions(A661ServerMC PRIVATE MODEL_COVERAGE TAGSET_MULTIROOT_INTEGRATION)
  target_link_libraries_twice(A661ServerMC ServerCore WidgetsMC)
  target_link_libraries(A661ServerMC ${GRAPHICAL_LIBRARIES} ${WINDOWING_LIBRARIES} ${SYSTEM_LIBRARIES})

  install_bin_target(A661ServerMC)

endif()

###############################################################################
# Target: A661ServerTest executable (for -testfw)
###############################################################################

if(${ENABLE_TESTFW})

  # ServerPlatformTest sources

  file(GLOB ServerPlatformTest_files ${SRVDIR}/platform/instrumented/*.[hc])
  set(ServerPlatformTest_includes ${SRVDIR}/platform/${PLATFORM_SUBDIR} ${SRVDIR}/platform/instrumented)
  source_group(TREE ${SRVDIR}/platform FILES ${ServerPlatformTest_files})

  # A661ServerTest executable

  if(TARGET ServerCore AND TARGET Widgets AND TARGET WidgetsTest AND EXISTS "${SRVDIR}/platform/${PLATFORM_SUBDIR}" AND EXISTS "${SRVDIR}/platform/instrumented")

    add_executable(A661ServerTest ${SRVDIR}/platform/${PLATFORM_SUBDIR}/exemain.c ${ServerPlatform_files} ${ServerPlatformTest_files})
    target_compile_definitions(A661ServerTest PRIVATE TEST_FRAMEWORK)
    target_include_directories(A661ServerTest PRIVATE ${ServerPlatformTest_includes})
    target_include_directories(A661ServerTest PRIVATE ${GENDIR}/srvgen)
    target_link_libraries_twice(A661ServerTest ServerCore Widgets WidgetsTest)
    target_link_libraries(A661ServerTest ${GRAPHICAL_LIBRARIES} ${WINDOWING_LIBRARIES} ${SYSTEM_LIBRARIES})

    install_bin_target(A661ServerTest)

  endif()

endif()

###############################################################################
# Target: A661ServerTestMC executable (for -testfw and -mc)
###############################################################################

if(${ENABLE_TESTFW})

  # A661ServerTestMC executable

  if(TARGET ServerCore AND TARGET WidgetsMC AND TARGET WidgetsTestMC AND EXISTS "${SRVDIR}/platform/${PLATFORM_SUBDIR}" AND EXISTS "${SRVDIR}/platform/instrumented")

    add_executable(A661ServerTestMC ${SRVDIR}/platform/${PLATFORM_SUBDIR}/exemain.c ${ServerPlatform_files} ${ServerPlatformTest_files})
    target_compile_definitions(A661ServerTestMC PRIVATE TEST_FRAMEWORK MODEL_COVERAGE TAGSET_MULTIROOT_INTEGRATION)
    target_include_directories(A661ServerTestMC PRIVATE ${ServerPlatformTest_includes})
    target_include_directories(A661ServerTestMC PRIVATE ${GENMCDIR}/srvgen)
    target_link_libraries_twice(A661ServerTestMC ServerCore WidgetsMC WidgetsTestMC)
    target_link_libraries(A661ServerTestMC ${GRAPHICAL_LIBRARIES} ${WINDOWING_LIBRARIES} ${SYSTEM_LIBRARIES})

    install_bin_target(A661ServerTestMC)

  endif()

endif()

###############################################################################
# Target: ServerUAPC library (for UAPC integration on Windows)
###############################################################################

if(${ENABLE_UAPC})

  # ServerUAPC library

  file(GLOB ServerUAPC_files ${SRVDIR}/platform/UAPC/*.[hc])
  list(FILTER ServerUAPC_files EXCLUDE REGEX "dllmain.c")
  list(LENGTH ServerUAPC_files ServerUAPC_length)

  file(GLOB ServerUAPC_hosted_files "${SRVDIR}/platform/hosted/utils.c" "${SRVDIR}/platform/hosted/monitoring.c" "${SRVDIR}/platform/hosted/timer.c")
  list(LENGTH ServerUAPC_hosted_files ServerUAPC_hosted_length)

  if((${ServerUAPC_length} GREATER 0) AND (${ServerUAPC_hosted_length} GREATER 0))
    # Use ServerUAPC sources

    if(DEFINED MANUAL_CODE_WARNINGS)
      set_source_files_properties(${ServerUAPC_files} PROPERTIES COMPILE_FLAGS ${MANUAL_CODE_WARNINGS})
    endif()

    set(ServerUAPC_includes ${SRVDIR}/platform/UAPC)

    source_group(TREE ${SRVDIR}/platform FILES ${ServerUAPC_files})

    add_library(ServerUAPC STATIC ${ServerUAPC_files} ${ServerUAPC_hosted_files})
    target_include_directories(ServerUAPC PRIVATE ${ServerUAPC_includes})

  else()
    # Use imported library
    import_library(ServerUAPC)

  endif()

endif()

###############################################################################
# Target: A661ServerUAPC DLL (for UAPC integration on Windows)
###############################################################################

if(${ENABLE_UAPC})

  # A661ServerUAPC shared library

  if(TARGET ServerUAPC AND TARGET ServerCore AND TARGET Widgets AND EXISTS "${SRVDIR}/platform/UAPC")

    add_library(A661ServerUAPC SHARED ${SRVDIR}/platform/UAPC/dllmain.c ${SRVDIR}/platform/UAPC/A661Server.def)
    set_target_properties(A661ServerUAPC PROPERTIES PREFIX "")
    target_link_libraries_twice(A661ServerUAPC ServerUAPC ServerCore Widgets)
    target_link_libraries(A661ServerUAPC ${GRAPHICAL_LIBRARIES} ${SYSTEM_LIBRARIES})

    install_bin_target(A661ServerUAPC)

  endif()

endif()

###############################################################################
